// package-activity/pages/activity-edit-delivery-select/index.js
const app = getApp();
import { PACKAGE_ACTIVITY_PAGE } from '../../../constants/route';
import { DELIVERY_TYPE, ACTIVITY_STATUS_VALUE, DELIVERY_TYPE_VALUE } from '../../../constants/activity';
import { navToAction, navBack, tipToast } from '../../../common/env';

Page({
    data: {
        DELIVERY_TYPE_VALUE,
        activityInfo: {},
        modifyFlag: true,
        selectInfo: {
            allList: DELIVERY_TYPE,
            has: {},
        },
    },
    onLoad(options) {
        app.event.addListener('sendActivityInfo', this._handleSendActivity.bind(this));
    },
    onUnload() {
        app.event.removeListener('sendActivityInfo');
    },
    _handleSendActivity(activityInfo) {
        const modifyFlag = !activityInfo.processStatus || activityInfo.processStatus === ACTIVITY_STATUS_VALUE.draft;
        this.setData({
            modifyFlag,
            activityInfo,
        });
        this._handleSelected(activityInfo.deliveryModes);
    },
    _handleSelected(list) {
        let { allList, has } = this.data.selectInfo;
        if (list) {
            if (list.length) {
                has.none = false;
                for (var k in allList) {
                    has[k] = list.findIndex(v => v === k) > -1;
                }
            } else {
                has = {
                    none: true
                };
            }
            this.setData({
                'selectInfo.has': has,
            });
        }
    },
    handleNavTo(e) {
        const { pageName, ...urlParams } = e.currentTarget ? e.currentTarget.dataset : e;
        const { activityInfo } = this.data;
        if (!this.data.modifyFlag && pageName === 'activityEditDelivery') {
            tipToast('活动已发布，此项不能编辑');
            return;
        }
        navToAction(PACKAGE_ACTIVITY_PAGE[pageName], {
            ...urlParams,
        }, {
            success(res) {
                setTimeout(() => {
                    app.event.trigger('sendActivityInfoDelivery', activityInfo);
                }, 800);
            },
        });
    },
    handleSelect(e) {
        if (!this.data.modifyFlag) {
            tipToast('活动已发布，此项不能编辑');
            return;
        }
        const { type } = e.currentTarget.dataset;
        let { deliveryModes = [] } = this.data.activityInfo;
        if (type === 'none') {
            deliveryModes = [];
        } else {
            let _index = deliveryModes.indexOf(type);
            if (_index > -1) {
                deliveryModes.splice(_index, 1);
            } else {
                deliveryModes.push(type);
            }
        }
        this.setData({
            'activityInfo.deliveryModes': deliveryModes,
        });
        this._handleSelected(deliveryModes);
    },
    handleSave() {
        if (!this.data.modifyFlag) {
            navBack();
            return;
        }
        const { activityInfo } = this.data;
        const { deliveryModes, warehouseIds, unifiedShippingFee, isUseWarehouseShippingFeeRule } = activityInfo;
        if (!deliveryModes || !deliveryModes.length) {
            tipToast('请选择配送方式');
            return;
        }
        if (deliveryModes && deliveryModes.indexOf(DELIVERY_TYPE_VALUE.express) > -1 && (unifiedShippingFee === undefined || unifiedShippingFee === null) && !isUseWarehouseShippingFeeRule) {
            tipToast('请设置配送费');
            return;
        }
        if (!warehouseIds || !warehouseIds.length) {
            tipToast('请选择供货仓');
            return;
        }
        app.event.trigger('getActivityInfo', activityInfo);
        navBack();
    },
})
